package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.ShopCarListResponse
import com.gexiaobao.pigeon.databinding.ItemShopCarBinding
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem

/**
 * @author: hxw
 * @date  : 2023/2/2
 * @desc  :
 */
class ShopCarAdapter(data: ArrayList<ShopCarListResponse.ListBean>) :
    BaseQuickAdapter<ShopCarListResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_shop_car, data) {

    private var isHaveInvalid: Int = 0
    private var mOnItemRemoveClickListener: OnItemRemoveClickListener? = null
    private var mOnItemDeleteClickListener: OnItemDeleteClickListener? = null

    private var mOnItemAddClickListener: OnItemAddClickListener? = null
    private var mOnItemReClickListener: OnItemReClickListener? = null
    private var mOnItemIvIconClickListener: OnItemIvIconClickListener? = null
    private var mOnItemSpecClickListener: OnItemSpecClickListener? = null
    private var mOnItemIvCheckClickListener: OnItemIvCheckClickListener? = null

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: ShopCarListResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemShopCarBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        if (item.check == 1) {
            holder.setImageResource(R.id.ivStoreSelected, R.mipmap.icon_good_check)
        } else {
            holder.setImageResource(R.id.ivStoreSelected, R.mipmap.icon_good_uncheck)
        }

        if (!item.isEmpty()) {
            item.list.forEachIndexed { _, listGoodsBean ->
                isHaveInvalid = if (listGoodsBean.isInvalid == 1) {//已失效
                    1
                } else {
                    0
                }
            }
        }

        holder.itemView.apply {
            binding.rvItemGoods.adapter = null
            if (item.isEmpty() || item.list.size == 0) {
                holder.setGone(R.id.rvItemGoods, true)//隐藏
                return
            }
            holder.setGone(R.id.rvItemGoods, false)//显示
            val mAdapterGoods = GoodsShoppingCartAdapter(item.list)
            binding.rvItemGoods.apply {
                val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
                    val width = resources.getDimensionPixelSize(R.dimen.dp_70)
                    val height = ViewGroup.LayoutParams.MATCH_PARENT
                    if (isHaveInvalid == 1) {
                        val removeItem = SwipeMenuItem(context)
                            .setBackground(R.color.comm_yellow)
                            .setTextColor(Color.WHITE)
                            .setText(context.getString(R.string.remove))
                            .setTextSize(16)
                            .setWidth(width)
                            .setHeight(height)
                        rightMenu.addMenuItem(removeItem)
                        holder.setGone(R.id.llCoupon, true)
                    } else {
                        holder.setGone(R.id.llCoupon, false)
                    }
                    val deleteItem = SwipeMenuItem(context)
                        .setBackground(R.color.delete_bg)
                        .setTextColor(Color.WHITE)
                        .setText(R.string.delete)
                        .setTextSize(16)
                        .setWidth(width)
                        .setHeight(height)
                    rightMenu.addMenuItem(deleteItem)

                }
                binding.rvItemGoods.setSwipeMenuCreator(swipeMenuCreator)

                val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
                    /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
                    menuBridge.closeMenu()
                    /** position:item在列表中的位置，menuBridge.position:菜单在Item中的Position.调用相关接口 */
                    if (isHaveInvalid == 1) {//已失效
                        if (menuBridge.position == 0) {//移除
                            /** 做移除操作 */
                        } else {
                            /** 删除 */
                            mOnItemDeleteClickListener?.onDeleteItemClick(mAdapterGoods.data[position].id)
                        }
                    } else {
                        mOnItemDeleteClickListener?.onDeleteItemClick(mAdapterGoods.data[position].id)
                    }
                }
                binding.rvItemGoods.setOnItemMenuClickListener(mItemMenuClickListener)
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapterGoods
                mAdapterGoods.notifyDataSetChanged()
            }
            mAdapterGoods.addChildClickViewIds(R.id.car_re, R.id.car_add, R.id.car_spec_ll, R.id.ivGoodsImage, R.id.ivShopCarGoodsChecked)
            mAdapterGoods.setOnItemChildClickListener { _, view, position ->
                when (view.id) {
                    R.id.car_re -> {
                        mOnItemReClickListener?.onReItemClick(mAdapterGoods.data[position].id)
                    }

                    R.id.car_add -> {
                        mOnItemAddClickListener?.onAddItemClick(mAdapterGoods.data[position].id)
                    }

                    R.id.car_spec_ll -> {
                        mOnItemSpecClickListener?.onSpecItemClick(mAdapterGoods.data[position].goodId, position, holder.layoutPosition)
                    }

                    R.id.ivGoodsImage -> {
                        mOnItemIvIconClickListener?.onIvIconItemClick(mAdapterGoods.data[position].goodId)
                    }

                    R.id.ivShopCarGoodsChecked -> {
                        mOnItemIvCheckClickListener?.onIvCheckItemClick(position, holder.layoutPosition)
                    }
                }
            }
        }
    }

    interface OnItemRemoveClickListener {
        fun onRemoveItemClick(id: Int)
    }

    fun setOnItemRemoveClickListener(onItemRemoveClickListener: OnItemRemoveClickListener) {
        mOnItemRemoveClickListener = onItemRemoveClickListener
    }

    interface OnItemDeleteClickListener {
        fun onDeleteItemClick(id: Int)
    }

    fun setOnItemDeleteClickListener(onItemDeleteClickListener: OnItemDeleteClickListener) {
        mOnItemDeleteClickListener = onItemDeleteClickListener
    }

    /** 增加 */
    interface OnItemAddClickListener {
        fun onAddItemClick(id: Int)
    }

    fun setOnItemAddClickListener(onItemAddClickListener: OnItemAddClickListener) {
        mOnItemAddClickListener = onItemAddClickListener
    }

    /** 减去 */
    interface OnItemReClickListener {
        fun onReItemClick(id: Int)
    }

    fun setOnItemReClickListener(onItemReClickListener: OnItemReClickListener) {
        mOnItemReClickListener = onItemReClickListener
    }

    /** 详情 */
    interface OnItemIvIconClickListener {
        fun onIvIconItemClick(id: Int)
    }

    fun setOnItemIvIconClickListener(onItemIvIconClickListener: OnItemIvIconClickListener) {
        mOnItemIvIconClickListener = onItemIvIconClickListener
    }

    /** 选择规格 */
    interface OnItemSpecClickListener {
        fun onSpecItemClick(id: Int, position: Int, parentPosition: Int)
    }

    fun setOnItemSpecClickListener(onItemSpecClickListener: OnItemSpecClickListener) {
        mOnItemSpecClickListener = onItemSpecClickListener
    }

    /** 选中 */
    interface OnItemIvCheckClickListener {
        fun onIvCheckItemClick(position: Int, parentPosition: Int)
    }

    fun setOnItemIvCheckClickListener(onItemIvCheckClickListener: OnItemIvCheckClickListener) {
        mOnItemIvCheckClickListener = onItemIvCheckClickListener
    }

}